import { OrientationType } from "./AdaptUI";


//这里添加已知刘海机型
// let topBangMachines = [
//     { matchName: "iPhone X", topOffset: 50 },
//     { matchName: "vivo X21A", topOffset: 50 },
// ];
//这里添加已知底部横条机型
// let bottomBarMachines = [
//     { matchName: "iPhone X", bottomOffset: 30 },
//     { matchName: "vivo X21A", bottomOffset: 30 },
//     { matchName: "M4 Pro", bottomOffset: 30 },
//     { matchName: "M463C", bottomOffset: 30 },
//     { matchName: "M351", bottomOffset: 30 },
// ];
export default class Resolution {

    static frameSize: cc.Size;  // 屏幕逐帧大小

    static designSize: cc.Size; // 设计大小

    static visibleSize: cc.Size;

    //屏幕分辨率宽高比
    static frameAspectRatio: number;

    //设计分辨率宽高比
    static designAspectRatio: number;

    //适配策略
    static orientation: number;

    static scale: number = 1;

    static offset = [];


    static fitType: SVGAnimatedNumber

    static bgRatio: number = 1;

    static contentRatio: number = 1;

    static init() {
        this.frameSize = cc.view.getFrameSize();
        console.log("屏幕宽高 : ", this.frameSize.width, this.frameSize.height);
        this.designSize = cc.Canvas.instance.designResolution;
        console.log("canvas宽高 : ", this.designSize.width, this.designSize.height);
        this.frameAspectRatio = this.frameSize.width / this.frameSize.height;
        console.log("屏幕宽高比: ", this.frameAspectRatio);
        this.designAspectRatio = this.designSize.width / this.designSize.height;
        console.log("canvas宽高比: ", this.designAspectRatio);
        this.orientation = this.designSize.height > this.designSize.width ? OrientationType.Portrait : OrientationType.Landscape;
        cc.log("屏幕横竖屏: ", this.orientation);
        console.log('可视宽高 ', cc.view.getVisibleSize());
        this.visibleSize = cc.view.getVisibleSize()
        let ws: number = this.frameSize.width / this.designSize.width;
        let hs: number = this.frameSize.height / this.designSize.height;

        let rw = this.visibleSize.width / this.designSize.width
        let rh = this.visibleSize.height / this.designSize.height;
        console.log(' rw  ', rw)
        console.log(' rh  ', rh)
        if (rw > rh) {
            this.bgRatio = rw;
        } else {
            this.bgRatio = rh;
        }

        if (rw > 1) {
            if (rw > rh) {
                this.contentRatio = rw;
            } else {
                this.contentRatio = rh;
            }
        } else {
            if (rw < rh) {
                this.contentRatio = rw;
            } else {
                this.contentRatio = rh;
            }
        }

        console.log(' this.bgRatio  ', this.bgRatio)
        if (ws > hs) {
            this.scale = ws;
        } else {
            this.scale = hs;
        }

        this.setOffset();
    }

    static getBgScale() {
        return this.bgRatio;
    }

    static getFDRatio() {
        let ratio = this.designAspectRatio / this.frameAspectRatio;
        if(ratio < 1){
            ratio = 1;
        }
        console.log("getFDRatio  ratio ", ratio)
        return ratio;
    }


    /**
     * 优先根据设配名称做适配，如果不知道的根据分辨率适配。
     */
    static setOffset() {
        let deltaAspectRatio = 1 / Resolution.frameAspectRatio - 1 / Resolution.designAspectRatio;
        console.log("deltaAspectRatio  ", deltaAspectRatio);
        // let machine = null;
        // if (cc.sys.platform == cc.sys.WECHAT_GAME) {
        //     machine = wx.getSystemInfoSync().model;
        // } else {
        //     //其他平台, 需要想办法拿到机型信息
        // }
        // cc.log("machine  ",machine);
        let topOffset = 0;
        // if(machine){
        //     for (let i = 0; i < topBangMachines.length; i++) {
        //         if (machine.search(topBangMachines[i].matchName) > -1) {
        //             topOffset = topBangMachines[i].topOffset;
        //             break;
        //         }
        //     }
        // }

        if (topOffset === 0 && deltaAspectRatio > 0) {
            topOffset = deltaAspectRatio * 120;
        }
        Resolution.offset[0] = topOffset;

        let bottomOffset = 0;
        // if(machine){
        //     for (let i = 0; i < bottomBarMachines.length; i++) {
        //         if (machine.search(bottomBarMachines[i].matchName) > -1) {
        //             bottomOffset = bottomBarMachines[i].bottomOffset;
        //             break;
        //         }
        //     }  
        // }
        if (bottomOffset === 0 && deltaAspectRatio > 0) {
            bottomOffset = deltaAspectRatio * 100;
        }
        Resolution.offset[1] = bottomOffset;
    }
    static getOffset() {
        return Resolution.offset;
    }

    static setRoot(root: cc.Node) {
        let offset = this.getOffset();
        cc.log("Resolution  offset ", offset);
        if (this.orientation == OrientationType.Portrait) {
            root.height -= offset[0];
            root.height -= offset[1];

        } else {
            root.width -= offset[0];
            root.width -= offset[1];
        }
    }

    static resize() {
        
        let dr = cc.view.getDesignResolutionSize();
        var s = cc.view.getFrameSize();
        var rw = s.width;
        var rh = s.height;
        var finalW = rw;
        var finalH = rh;

        if ((rw / rh) > (dr.width / dr.height)) {
            //!#zh: 是否优先将设计分辨率高度撑满视图高度。 */
            //cvs.fitHeight = true;

            //如果更长，则用定高
            finalH = dr.height;
            finalW = finalH * rw / rh;
        }
        else {
            /*!#zh: 是否优先将设计分辨率宽度撑满视图宽度。 */
            //cvs.fitWidth = true;
            //如果更短，则用定宽
            finalW = dr.width;
            finalH = rh / rw * finalW;
        }

        cc.view.setDesignResolutionSize(finalW, finalH, cc.ResolutionPolicy.UNKNOWN);
        let cvs: cc.Canvas = cc.find('Canvas').getComponent(cc.Canvas);
        cvs.node.width = finalW;
        cvs.node.height = finalH;
    }
}
